package com.woniuxy.mapper;

import java.util.List;

import org.apache.ibatis.annotations.*;

import com.woniuxy.entity.CategoryOne;
import com.woniuxy.entity.CategoryTwo;
import com.woniuxy.entity.HeatBidding;
import com.woniuxy.entity.Product;

@Mapper
public interface ProductMapper {

	// @Select("select picture from product where now() between auction_time and
	// date_add(now(), interval 30 minute)")
	// @Select("select picture,b.price as currentPrice,count(b.price) as
	// biddingTimes from bidding b,product p where p.p_id=b.p_id and(now() between
	// auction_time and date_add(now(), interval 30 minute)) order by heat desc" )
	@Select("select p_id,picture,name from product where (now() between auction_time and date_add(auction_time, interval 30 minute))")
	@Results({ @Result(id = true, column = "b_id", property = "b_id"),
			@Result(column = "p_id", property = "biddings", one = @One(select = "com.woniuxy.mapper.BiddingMapper.allBiddings")),
			@Result(column = "p_id", property = "currentPrice", one = @One(select = "com.woniuxy.mapper.BiddingMapper.currentPrice")) })
	public List<HeatBidding> findHeatBidding();// 查询当前热拍卖

	@Select("select * from product where p_id=#{p_id}")
	public Product findProductById(int p_id, int u_id);// 查询指定拍卖品

	// @SelectProvider(type=null,method=)
	@Select("select distinct p_id,co_id,ct_id,picture,name,starting_price,auction_time,"
			+ "audit_status from product where co_id=#{co_id}")
	public List<Product> findByCategory(int co_id);

//	@SelectProvider(type=ProductMapperProvider.class,method="findByCt_id")
	@Select("select p_id,co_id,ct_id,picture,name,starting_price,auction_time,"
			+ "audit_status from product where co_id=#{co_id} and ct_id=#{ct_id}")
	public List<Product> findByCt_id(Product product);

	@Select("select ct_id from category_two where name=#{name}")
	public int findCt_idByName(String name);

	@Select("select p_id,co_id,ct_id,picture,name,starting_price,auction_time,"
			+ "audit_status from product where ct_id=#{ct_id}")
	public List<Product> findByName(int ct_id);

	@Select("select p_id,co_id,ct_id,picture,name,starting_price,auction_time,"
			+ "audit_status from product where audit_status=#{audit_status}")
	public List<Product> findByStatus(int audit_status);

	@Select("select p_id,co_id,ct_id,picture,name,starting_price,auction_time,"
			+ "audit_status from product where ct_id=#{ct_id} and audit_status=#{audit_status}")
	public List<Product> findByStatusAndName(Product product);

	@Select("select p_id,co_id,ct_id,picture,name,starting_price,auction_time,"
			+ "audit_status,details from product order by heat desc limit 0,8")
	public List<Product> findHeat();

	@Select("select p_id,co_id,ct_id,picture,name,starting_price,auction_time,"
			+ "audit_status,details from product order by heat desc limit 0,3")
	public List<Product> findHeat2();

	@Select("select p_id,co_id,ct_id,picture,name,starting_price,auction_time,"
			+ "audit_status from product where name like #{name}")
	public List<Product> findByLike(String name);

	@Select("select p_id,co_id,ct_id,picture,name,starting_price,auction_time,"
			+ "audit_status from product where co_id=#{co_id} order by date_issued desc")
	public List<Product> findByNewest(int co_id);

	@Select("select p_id,co_id,ct_id,picture,name,starting_price,auction_time,"
			+ "audit_status from product where co_id=#{co_id} order by heat desc")
	public List<Product> findByHotest(int co_id);

	@Select("select p.p_id,co_id,ct_id,picture,name,starting_price,auction_time,"
			+ "audit_status,charge_num from (SELECT * FROM "
			+ "(SELECT COUNT(*) charge_num,p_id FROM bidding GROUP BY p_id) a"
			+ "	ORDER BY charge_num DESC) b LEFT JOIN product p ON b.p_id=p.p_id and co_id=#{co_id}")
	public List<Product> findByChargeNum(int co_id);

	@Select("select p_id,co_id,ct_id,picture,name,starting_price,auction_time,"
			+ "audit_status from product where co_id=#{co_id} order by starting_price desc")
	public List<Product> findByPriceDown(int co_id);

	@Select("select p_id,co_id,ct_id,picture,name,starting_price,auction_time,"
			+ "audit_status from product where co_id=#{co_id} order by starting_price")
	public List<Product> findByPriceUp(int co_id);

	@Select("select count(*) from collect where p_id=#{p_id} and u_id=#{u_id}")
	public int isCollected(int p_id, int u_id);// 查询是否收藏过商品

	@Select("select count(*) from sign where p_id=#{p_id} and u_id=#{u_id}")
	public int isSigned(int p_id, int u_id);// 查询是否预约过商品

	@Select("select * from product where u_id = #{u_id}")
	public List<Product> goodesByUid(int uid);

	@Select("select * from category_one")
	public List<CategoryOne> findoneMenu();

	@Select("select * from category_two")
	public List<CategoryTwo> findTwoMenu();

	@Insert("insert into product values(default,#{name},#{num},#{picture},#{u_id},null,#{ct_id},'0',1,#{date_issued},#{auction_time},#{starting_price},null,#{deposit},#{bid_increment},#{details},#{auction_site})")
	public boolean addProduct(Product product);

	@Select("select * from category_two where co_id=#{co_id}")
	public List<CategoryTwo> findCategory_two(int co_id);

	@Update("update product set heat=heat+1 where p_id=#{p_id}")
	public int updateHeat(int p_id);

	@Select("select * from product where p_id=#{pid}")
	public Product find(int pid);

	@Select("select*from product")
	public List<Product> allProducts();

	// 批量修改商品
	@Update({ "<script> UPDATE product SET audit_status = #{audit_status} " + "WHERE p_id IN "
			+ "<foreach  collection = 'ids' item = 'p_id' index = 'index' open = '(' separator= ',' close = ')' >"
			+ "	#{p_id} " + "</foreach>" + "</script>" })
	public int batchUpdateProducts(@Param("audit_status") Integer audit_status, @Param("ids") int[] ids);

	// 批量删除商品
	@Update({ "<script> DELETE FROM product WHERE p_id IN "
			+ "<foreach  collection = 'ids' item = 'p_id' index = 'index' open = '(' separator= ',' close = ')' >"
			+ "	#{p_id} " + "</foreach>" + "</script>" })
	public int batchDeleteProducts(@Param("ids") int[] ids);

}
